""" 
Hunger Games 
Stuart Bradley 
18-07-2013

NUMBER OF PLAYERS [P]
FOOD INIT [F] = 300(P-1)
REP INIT [R] = 0
HUNTS MAX = P-1
    HUNT = 'h' F-6
    SLACK = 's' F-2
    IF COOP HUNT 
        FOOD = FOOD
    IF NO HUNT
        FOOD -= 2
    IF UNCOOP HUNT 
        FOOD -= 3

REP = (HUNTS)/(HUNTS + SLACKS)
    IF TOTAL HUNTS >= M {RAND(0 -> P(P-1))}
        FOOD += 2(P-1) 
        
BOARD: {U : USER, T : TRIBE, E : ENEMY}
    
    [U][E]
    [T][E]
    [T][E]
    [T][E]
    [T][E]
    
RETURNS ARRAY OF HUNT 'h' OR SLACK 's' ON ENEMY TRIBE

Lazy coding and 
programs aren't great. But today:
Adapt or die 
"""

import random

netFoodChange = 0
prevhunt_decisions = []
prevhunt_reputations = []
prevhunt_cooperators = 0
prev_foodearnings =[]

def hunt_choices(round_number, current_food, current_reputation, m,
            player_reputations):
    
    #First round, init and hunt to test algorithms
    if round_number == 1:
        prevhunt_decisions = ['h' for x in player_reputations]
        prevhunt_reputations = player_reputations
        return prevhunt_decisions
    
    #if current behaviour is working, continue to use it.         
    if netFoodChange > 0:
        prevhunt_reputations = player_reputations
        hunt_decisions = prevhunt_decisions

    else:
        #if last round enough algorithm hunted to beat current round's 'm', slack to save food
        if (m < (prevhunt_cooperators-1)) and (current_reputation >= 0):
            hunt_decisions = ['s' for x in player_reputations]
        else:
            #changes 'h'/'s' based on where food was lost in last round
            for index, i in enumeration(food_earnings):
                if i < prevhunt_foodearnings[index]:
                    if hunt_decisions[index] == 'h':
                        hunt_decisions[index] = 's'
                    else:
                        hunt_decisions[index] = 'h'

    #stops stagnation and while netFood change is above 0
    if sum(player_reputations) >= sum(prevhunt_reputations):
        while True:
            rand_int = random.randrange(0,len(hunt_decisions))
            if hunt_decisions[rand_int] == 's':
                hunt_decisions[rand_int] = 'h'
                break
    else:
        while True:
            rand_int = random.randrange(0,len(hunt_decisions))
            if hunt_decisions[rand_int] == 'h':
                hunt_decisions[rand_int] = 's'
                break

    #sets next round, returns 
    prevhunt_decisions = hunt_decisions
    prevhunt_reputations = player_reputations
    return hunt_decisions

def hunt_outcomes(food_earnings):
    prev_foodearnings = food_earnings
    netFoodChange = 0
    for i in food_earnings:
        netFoodChange += i

def round_end(award, m, number_cooperators):
    prevhunt_cooperators = number_cooperators